let { HttpRequest, globalData, UserAccess, addUserStat, mfutil,URLChangeCommon } = getApp();
const app = getApp();
Page({
  data: {
    id: null,


    radioActive: false,
    router: '/pages/store_page/store_page',
    pagetitle: '门店自提',
    loadingActive: false
  },
  onLoad(options) {
    my.hideShareMenu({
      success() {
      },
      fail() {
      },
    });
    console.log(options, "1111111111111111111111111111111111111！！！！！！");
    let { id, lucky_image, lucky_text, prize_name, prize_sku, cta,done_id } = options;
    this.setData({
      id, lucky_image, lucky_text, prize_name, sku:prize_sku, cta:JSON.parse(cta),done_id
    })
    this.initPage()
    /**
* 用户行为记录埋点
*/
    let routerArr = getCurrentPages();
    let params = app.globalData.params;
    let channel = app.globalData.channel;
    let UserAccessoptions = {
      grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
      name: routerArr[routerArr.length - 1].data.pagetitle,
      address: options.id,
    }
    let track = UserAccess(UserAccessoptions)
    // 自定义埋点
    let customAccessoptions = {
      customType: 'TileCover_' + options.id,
      // tileId: id
    }
    console.log(id, '进入抽奖详情页');
    let tracks = addUserStat(customAccessoptions)
  },
  onShow() {
    this.initPage()
  },
  async initPage() {
    try {
      let { id,sku } = this.data;

      let path = `/vans-family/api/v2.1/project/vansFamily/cms/forward/get?url=/v2/cms/api/tile/${id}`;
      let parmasObj = {
        member_id_type: 1,
        member_id:globalData.userInfo.memberId
      }
      // this.setData({
      //   loadingActive: false
      // })
      let res = await HttpRequest(path, 'POST', parmasObj);
      console.log(res, "&&&&&&&&&&&&&&&&&&&&&&&&&&&");
      if (res.data.code == 0) {
        let tilesDetail = res.data.data;
        console.log(tilesDetail.tile_info,"-------------0");
        this.setData({
          tilesDetail,
          complete_text:tilesDetail.tile_info.complete_text
          // skuid: res.data.data.tile_info.prize_items[0].sku,
        })
        // console.log(type, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@22");
        // if (tilesDetail.tile_info.prize_items.prize_type == 'store' || tilesDetail.tile_info.prize_items.prize_type == 'all') {
          let storespath = `/vans-family/api/v2.1/project/vansFamily/cms/forward/get?url=/v1/cms/api/get_available_stores`;

          let storesparmasObj = {
            member_id_type: 1,
            member_id: globalData.userInfo.memberId,
            tile_id: id,
              sku
          }
          console.log(storesparmasObj, "==============storesparmasObjstoresparmasObjstoresparmasObj=================--");
          let storesres = await HttpRequest(storespath, 'POST', storesparmasObj);
          console.log(storesres, "===============================--");
          // if (storesres.data.code == 0) {
          this.setData({
            storeList: storesres.data.data
          })
          // }
        // }

      }
      this.setData({
        loadingActive: false
      })
    } catch (earr) {
      console.log(earr);
    }

  },
  /**
   * 门店自提
   * @param {*} event 
   */
  pick() {
    let { id,  sku, storeList,done_id,cta,lucky_text,lucky_image,complete_text} = this.data;
    if (storeList.length == 0) {
      my.alert({
        content: '库存不足，无法参与兑礼!'
      })
      return
    }
    my.redirectTo({ //storeExpress-form
      url: `/pages/storeExpress-form/storeExpress-form?method=pick&id=${id}&skuid=${sku}&active=0&done_id=${done_id}&cta=${encodeURIComponent(JSON.stringify(cta))}&lucky_text=${lucky_text}&lucky_image=${lucky_image}&complete_text=${complete_text}`
    });
    /**
   * 用户行为记录埋点
   */
    let routerArr = getCurrentPages();
    let params = app.globalData.params;
    let channel = app.globalData.channel;
    let options = {
      grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
      name: routerArr[routerArr.length - 1].data.pagetitle,
      address: routerArr[routerArr.length - 1].data.router,
      String4: params ? `params=${params.join(',')}&` : '' + `tileid=${this.data.id}${channel ? '&channel=' + channel : ''}`,
      String3: '门店自提'
    }
    let track = UserAccess(options)
  },

  //单选协议同意/默认
  radioChange() {
    let { radioActive } = this.data;
    this.setData({
      radioActive: !radioActive
    })
  },
  // btn 链接跳转
  urlChange(event) {


    let { item } = event.currentTarget.dataset;
    console.log(item);

    URLChangeCommon(item)
    /**
* 用户行为记录埋点
*/
    let routerArr = getCurrentPages();
    let params = app.globalData.params;
    let channel = app.globalData.channel;
    let UserAccessoptions = {
      grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
      name: routerArr[routerArr.length - 1].data.pagetitle,
      address: this.data.id,
      String4: item.cta_button_name,

    }
    let track = UserAccess(UserAccessoptions)

    // 自定义埋点
    let customAccessoptions = {
      customType: 'TileCTA_' + this.data.tilesDetail.tile_info.id + '_' + item.cta_button_name
      // tileId: this.data.tilesDetail.tile_info.id,
      // ctabtn: this.data.tilesDetail.tile_info.cta[0].cta_button_name
    }

    let tracks = addUserStat(customAccessoptions)
  },
});
